package com.java110.mall.goods.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.product.ProductStockDto;
import com.java110.mall.goods.bmo.productStock.IDeleteProductStockBMO;
import com.java110.mall.goods.bmo.productStock.IGetProductStockBMO;
import com.java110.mall.goods.bmo.productStock.ISaveProductStockBMO;
import com.java110.mall.goods.bmo.productStock.IUpdateProductStockBMO;
import com.java110.po.product.ProductStockPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/productStock")
public class ProductStockApi {

    @Autowired
    private ISaveProductStockBMO saveProductStockBMOImpl;
    @Autowired
    private IUpdateProductStockBMO updateProductStockBMOImpl;
    @Autowired
    private IDeleteProductStockBMO deleteProductStockBMOImpl;

    @Autowired
    private IGetProductStockBMO getProductStockBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /productStock/saveProductStock
     * @path /app/productStock/saveProductStock
     */
    @RequestMapping(value = "/saveProductStock", method = RequestMethod.POST)
    public ResponseEntity<String> saveProductStock(
            @RequestHeader(value = "user-id", required = false) String userId,
            @RequestHeader(value = "user-name", required = false) String userName,

            @RequestBody JSONObject reqJson
    ) {

        Assert.hasKeyAndValue(reqJson, "barCode", "请求报文中未包含barCode");
        Assert.hasKeyAndValue(reqJson, "productId", "请求报文中未包含productId");
        Assert.hasKeyAndValue(reqJson, "valueId", "请求报文中未包含规格");


        ProductStockPo productStockPo = BeanConvertUtil.covertBean(reqJson, ProductStockPo.class);
        productStockPo.setCreateUserId(userId);
        productStockPo.setCreateUserName(userName);
        productStockPo.setState("1001");
        return saveProductStockBMOImpl.save(productStockPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /productStock/updateProductStock
     * @path /app/productStock/updateProductStock
     */
    @RequestMapping(value = "/updateProductStock", method = RequestMethod.POST)
    public ResponseEntity<String> updateProductStock(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "barCode", "请求报文中未包含barCode");
        Assert.hasKeyAndValue(reqJson, "productId", "请求报文中未包含productId");
        Assert.hasKeyAndValue(reqJson, "psId", "psId不能为空");


        ProductStockPo productStockPo = BeanConvertUtil.covertBean(reqJson, ProductStockPo.class);
        return updateProductStockBMOImpl.update(productStockPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /productStock/deleteProductStock
     * @path /app/productStock/deleteProductStock
     */
    @RequestMapping(value = "/deleteProductStock", method = RequestMethod.POST)
    public ResponseEntity<String> deleteProductStock(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopId", "商铺ID不能为空");

        Assert.hasKeyAndValue(reqJson, "psId", "psId不能为空");


        ProductStockPo productStockPo = BeanConvertUtil.covertBean(reqJson, ProductStockPo.class);
        return deleteProductStockBMOImpl.delete(productStockPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 商铺ID
     * @return
     * @serviceCode /productStock/queryProductStock
     * @path /app/productStock/queryProductStock
     */
    @RequestMapping(value = "/queryProductStock", method = RequestMethod.GET)
    public ResponseEntity<String> queryProductStock(@RequestParam(value = "shopId") String shopId,
                                                    @RequestParam(value = "productId", required = false) String productId,
                                                    @RequestParam(value = "valueId", required = false) String valueId,
                                                    @RequestParam(value = "page") int page,
                                                    @RequestParam(value = "row") int row,
                                                    @RequestParam(value = "barCode", required = false) String barCode) {
        ProductStockDto productStockDto = new ProductStockDto();
        productStockDto.setPage(page);
        productStockDto.setRow(row);
        productStockDto.setShopId(shopId);
        productStockDto.setBarCode(barCode);
        productStockDto.setProductId(productId);
        productStockDto.setValueId(valueId);
        return getProductStockBMOImpl.get(productStockDto);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 商铺ID
     * @return
     * @serviceCode /productStock/searchProductStock
     * @path /app/productStock/searchProductStock
     */
    @RequestMapping(value = "/searchProductStock", method = RequestMethod.GET)
    public ResponseEntity<String> searchProductStock(@RequestParam(value = "shopId") String shopId,
                                                     @RequestParam(value = "page") int page,
                                                     @RequestParam(value = "row") int row,
                                                     @RequestParam(value = "searchValue", required = false) String searchValue) {
        ProductStockDto productStockDto = new ProductStockDto();
        productStockDto.setPage(page);
        productStockDto.setRow(row);
        productStockDto.setShopId(shopId);
        productStockDto.setBarCode(searchValue);
        return getProductStockBMOImpl.search(productStockDto);
    }
}
